<template>
  <div class="container">
    <div class="container-content">
      <vab-query-form>
        <vab-query-form-top-panel>
          <el-form
            :inline="true"
            label-width="80px"
            :model="tableOptions.queryForm"
          >
            <el-form-item label="配置编码">
              <el-input
                v-model="tableOptions.queryForm.treeCode"
                placeholder="配置编码"
              />
            </el-form-item>
            <el-form-item label="配置名称">
              <el-input
                v-model="tableOptions.queryForm.treeName"
                placeholder="配置名称"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                icon="refresh-left"
                type="warning"
                @click="handleResetQuery"
              >
                重置
              </el-button>
              <el-button icon="search" type="primary" @click="queryTableData">
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </vab-query-form-top-panel>
        <vab-query-form-left-panel :span="24">
          <el-button
            v-auth="['systemtree/xz']"
            icon="circle-plus"
            type="primary"
            @click="handleAdd"
          >
            添加
          </el-button>
          <el-button
            v-auth="['systemtree/sc']"
            icon="remove"
            type="danger"
            @click="handleDelete"
          >
            批量删除
          </el-button>
        </vab-query-form-left-panel>
      </vab-query-form>

      <el-table
        ref="tableRef"
        v-loading="tableOptions.loading"
        border
        stripe
        :data="tableOptions.dataList"
        :height="tableOptions.height"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
      >
        <el-table-column
          align="center"
          show-overflow-tooltip
          type="selection"
          width="60"
        />
        <el-table-column label="序号" type="index" width="60">
          <template #default="{ $index }">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="配置名称" prop="treeName" />
        <el-table-column align="center" label="配置编码" prop="treeCode" />
        <el-table-column
          align="center"
          label="系统配置"
          prop="systemTreeDesc"
        />
        <el-table-column align="center" fixed="right" label="操作" width="240">
          <template #default="{ row }">
            <el-button
              v-auth="['systemtree/jdgl']"
              size="small"
              type="primary"
              @click="handleNodeDetail(row)"
            >
              配置数据
            </el-button>
            <el-button
              v-auth="['systemtree/xg']"
              size="small"
              type="warning"
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              v-auth="['systemtree/sc']"
              size="small"
              type="danger"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="tableOptions.pagination.current"
        :layout="tableOptions.pagination.layout"
        :page-size="tableOptions.pagination.size"
        :total="tableOptions.pagination.total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
      <system-tree-edit ref="editRef" width="40%" @confirm="queryTableData" />
      <system-tree-node
        ref="treeNodeRef"
        width="60%"
        @confirm="queryTableData"
      />
    </div>
  </div>
</template>
<script setup name="Systemtree">
import { useMessage, useMessageBox } from "@/hooks/message";
import {
  querySystemTreeTable,
  deleteSystemTree
} from "@/api/admin/system/systemtree/";
import SystemTreeEdit from "./components/systemtreeEdit.vue";
import SystemTreeNode from "./components/systemtreeNode.vue";
import { useTable } from "@/hooks/table";
import { getTableHeight } from "@/utils/util";

const editRef = ref(null);
const tableRef = ref(null);
const treeNodeRef = ref(null);

const tableOptions = reactive({
  autoLoad: true,
  queryForm: {
    treeCode: "",
    treeName: ""
  },
  queryFunction: querySystemTreeTable,
  ascs: ["tree_code"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, tableRef);

const handleAdd = () => {
  editRef.value.show();
};

const handleDelete = row => {
  if (row && row.id) {
    useMessageBox()
      .confirm("删除的配置将无法恢复，是否继续")
      .then(async () => {
        deleteSystemTree(row.id).then(res => {
          if (res.code === 200) {
            useMessage().success(res.msg);
            queryData();
          } else {
            useMessage().error(res.msg);
          }
        });
      });
  } else {
    if (tableOptions.dataSelections.length > 0) {
      const ids = tableOptions.dataSelections.map(item => item.id).join();
      useMessageBox()
        .confirm("删除的配置将无法恢复，是否继续")
        .then(async () => {
          deleteSystemTree(ids).then(res => {
            if (res.code === 200) {
              useMessage().success(res.msg);
              queryData();
            } else {
              useMessage().error(res.msg);
            }
          });
        });
    } else {
      useMessage().error("未选中任何行");
    }
  }
};

const handleEdit = row => {
  if (row.id) {
    editRef.value.show(row, false);
  } else {
    useMessage().error("未选中任何行");
  }
};

const handleNodeDetail = row => {
  console.log("handleNodeDetail", row, treeNodeRef.value);
  if (row.id) {
    treeNodeRef.value.show(row);
  } else {
    useMessage().error("未选中任何行");
  }
};
</script>
<style lang="scss" scoped></style>
